package lijun.parking;

import lijun.parking.Entity.Admin;
import lijun.parking.Entity.Car;

import java.sql.SQLOutput;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class ParkingMS {
    private static List<Admin> adminList = new LinkedList<>();
    private static List<Car> carList = new LinkedList<>();
    private static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        adminList.add(new Admin());
        Car car1 = new Car("1", "粤A-123456", "大型车辆", true, new Date(Date.parse("Mon 25 Jun 2022 8:3:00")), 0, false, new Date());
        Car car2 = new Car("2", "云C-123456", "新能源车辆", false, new Date(Date.parse("Mon 25 Jun 2022 9:30:00")), 100, true, new Date());
        Car car3 = new Car("3", "贵H-123456", "中小型车辆", false, new Date(Date.parse("Mon 24 Jun 2022 10:10:00")), 100, true, new Date());
        carList.add(car1);
        carList.add(car2);
        carList.add(car3);

        login();
    }

    public static void login() {
        System.out.println("==========停车场管理系统(登录页面)==========");
        System.out.println("请输入账号:");
        String username = sc.next();
        System.out.println("请输入密码:");
        String password = sc.next();

        for (Admin admin : adminList) {
            if (admin.getUserName().equals(username)) {
                if (admin.getPassword().equals(password)) {
                    parking();
                    return;
                } else {
                    System.err.println("密码错误！自动退出系统！");
                    return;
                }
            }
        }
        System.err.println("用户不存在！自动退出系统！");
    }

    public static void parking() {
        System.out.println("==========欢迎使用停车场管理系统！==========");
        System.out.println("请选择功能：");
        System.out.println("1.密码管理");
        System.out.println("2.车辆管理");
        System.out.println("3.车辆进出计费管理");
        System.out.println("4.返回登录页面");

        int i = sc.nextInt();

        if (i == 1) {
            passwordManager();
        }
        if (i == 2) {
            carManager();
        }
        if (i == 3) {
            carPriceManager();
        }
        if (i == 4) {
            login();
        }

    }

    public static void carPriceManager() {
        System.out.println("==========欢迎使用停车场管理系统！==========");
        System.out.println("请输入要出入车的车牌id：");
        String carId = sc.next();
        int index = getIndexByCarid(carId);
        if (index == -1) {
            System.err.println("车辆不存在！");
        } else {
            Car car = carList.get(index);
            System.out.println("请选择出入车：");
            System.out.println("1.车进入");
            System.out.println("2.车离开");
            System.out.println("3.返回主界面");
            int i = sc.nextInt();
            if (i == 1) {
                car.setEnterTime(new Date());
                car.setLeaveTime(new Date());
                System.out.println("车辆入车成功！");
            }
            if (i == 2) {
                car.setLeaveTime(new Date());
                long min = getDatePoor(car.getLeaveTime(), car.getEnterTime());
                long hour = min / 60;
                long day = hour / 24;
                String carType = car.getCarType();
                double price = 0;
                if (carType.equals("中小型车辆")) {
                    if (car.isBaoyue()) {
                        price = 480;
                    } else {
                        if (min <= 30) {
                            price = 0;
                        } else if (min <= 60 * 3) {
                            price = 5;
                        } else if (min > 60 * 3 && day == 0) {
                            price = Math.min((5 + (hour - 3) + 1), 20);
                        } else if (day > 0) {
                            price = Math.min((5 + (hour % 24 - 3) + 1), 20) + 20 * day;
                        }
                    }
                }
                if (carType.equals("大型车辆")) {
                    if (car.isBaoyue()) {
                        price = 960;
                    } else {
                        if (min <= 30) {
                            price = 0;
                        } else if (min <= 60 * 2) {
                            price = 10;
                        } else if (min > 60 * 2 && day == 0) {
                            price = Math.min((10 + (hour - 2 + 1) * 3), 60);
                        } else if (day > 0) {
                            price = Math.min((10 + (hour % 24 - 2 + 1) * 3), 60) + 60 * day;
                        }
                    }
                }
                if (carType.equals("新能源车辆")) {
                    if (car.isBaoyue()) {
                        price = 360;
                    } else {
                        if (min <= 60 * 2) {
                            price = 0;
                        } else if (min <= 60 * 4) {
                            price = 3;
                        } else if (min > 60 * 4 && day == 0) {
                            price = Math.min((3 + (hour - 4 + 1)), 20);
                        } else if (day > 0) {
                            price = Math.min((3 + (hour % 24 - 4 + 1) * 3), 20) + 20 * day;
                        }
                    }
                }
                System.out.println("车辆离开成功！停车时间为：" + min + " 分钟，即 " + day + "天" + hour % 24 + "小时"
                        + min % 60 + "分钟, 需付费: " + price + " 元");
            }
            if (i == 3) {
                parking();
            }
        }

        System.out.println("1:返回主界面 2:返回上一层");
        int ii = sc.nextInt();
        if (ii == 1) {
            parking();
        }
        if (ii == 2) {
            carPriceManager();
        }
    }

    public static void passwordManager() {
        System.out.println("==========欢迎使用停车场管理系统！==========");
        System.out.println("请选择密码修改对象：");
        System.out.println("1.自我");
        System.out.println("2.普通用户");
        System.out.println("3.返回主界面");
        int i = sc.nextInt();
        if (i == 1) {
            System.out.println("请输出新密码:");
            String newPassword = sc.next();
            alterPassword("admin", newPassword);
        }
        if (i == 2) {
            System.out.println("请输出普通用户账号:");
            String username = sc.next();
            System.out.println("请输出新密码:");
            String newPassword = sc.next();
            alterPassword("admin", newPassword);
        }
        if (i == 3) {
            parking();
        }

        System.out.println("1:返回主界面 2:返回上一层");
        int ii = sc.nextInt();
        if (ii == 1) {
            parking();
        }
        if (ii == 2) {
            passwordManager();
        }
    }

    public static void alterPassword(String username, String newPassword) {
        for (Admin admin : adminList) {
            if (admin.getUserName().equals(username)) {
                admin.setPassword(newPassword);
                System.out.println("密码修改成功！");
            }
        }
        System.err.println("用户不存在！");
    }

    public static void carManager() {
        System.out.println("==========欢迎使用停车场管理系统！==========");
        System.out.println("请选择车辆管理功能：");
        System.out.println("1.列出所有车辆信息");
        System.out.println("2.删除车辆信息");
        System.out.println("3.查询车辆信息");
        System.out.println("4.返回主界面");
        int i = sc.nextInt();
        if (i == 1) {
            System.out.println("车辆id |   车牌号   | 车辆类型 | 是否包月 |      车辆进入时间      | 车辆收费单价 | 是否在场 |   车辆离开时间  ");
            for (Car car : carList) {
                System.out.println(car.toString());
            }
        }
        if (i == 2) {
            System.out.println("请输出要删除的车辆id:");
            String carId = sc.next();
            deleteCar(carId);
        }
        if (i == 3) {
            System.out.println("请选择车辆查询条件：");
            System.out.println("1.车辆id");
            System.out.println("2.车牌号");
            System.out.println("3.全部车辆信息");
            int ii = sc.nextInt();
            if (ii == 1) {
                System.out.println("请输入车辆id");
                String carId = sc.next();
                int index = getIndexByCarid(carId);
                if (index == -1) {
                    System.err.println("车辆不存在！");
                } else {
                    System.out.println(carList.get(index).toString());
                }
            }
            if (ii == 2) {
                System.out.println("请输入车牌号");
                String carNumber = sc.next();
                int index = -1;
                for (int inn = 0; inn < carList.size(); inn++) {
                    if (carList.get(inn).getCarNumber().equals(carNumber)) {
                        index = inn;
                    }
                }
                if (index == -1) {
                    System.err.println("车辆不存在！");
                } else {
                    System.out.println("车辆id |   车牌号   | 车辆类型 | 是否包月 |    车辆进入时间    | 车辆收费单价 | 是否在场 |   车辆离开时间  ");
                    System.out.println(carList.get(index).toString());
                }
            }
            if (ii == 3) {
                System.out.println("车辆id |   车牌号   | 车辆类型 | 是否包月 |    车辆进入时间    | 车辆收费单价 | 是否在场 |   车辆离开时间  ");
                for (Car car : carList) {
                    System.out.println(car.toString());
                }
            }
        }
        if (i == 4) {
            parking();
        }

        System.out.println("1:返回主界面 2:返回上一层");
        int ii = sc.nextInt();
        if (ii == 1) {
            parking();
        }
        if (ii == 2) {
            carManager();
        }
    }

    public static void deleteCar(String carId) {
        int index = getIndexByCarid(carId);
        if (index == -1) {
            System.err.println("车辆不存在！");
        } else {
            System.out.println("确定删除当前车辆吗? 1:确定 2:取消");
            int delete = sc.nextInt();
            if (delete == 1) {
                carList.remove(index);
                System.out.println("删除车辆成功! ");
            } else {
                System.out.println("取消删除车辆!");
            }
        }
    }


    public static int getIndexByCarid(String carId) {
        int index = -1;
        for (int inn = 0; inn < carList.size(); inn++) {
            if (carList.get(inn).getCarId().equals(carId)) {
                index = inn;
            }
        }
        return index;
    }

    public static long getDatePoor(Date endDate, Date startDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // 获得两个时间的毫秒时间差异
        long time = endDate.getTime() - startDate.getTime();

        int minutes = (int) ((time) / (1000 * 60));
        return minutes;
    }


}
